package logic

import (
	"context"
	"jin-quan-dev/models/mysql"

	"jin-quan-dev/jinrpc/internal/svc"
	"jin-quan-dev/jinrpc/jinrpc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type UserAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserAddLogic {
	return &UserAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserAddLogic) UserAdd(in *jinrpc.UserAddRequest) (*jinrpc.UserAddResponse, error) {
	// todo: add your logic here and delete this line
	user := mysql.User{
		Model:    gorm.Model{},
		Username: in.Username,
		Password: in.Password,
	}
	err := user.UserAdd(mysql.DB)
	if err != nil {
		return nil, err
	}
	return &jinrpc.UserAddResponse{
		UserId: int64(user.ID),
	}, nil
}
